package com.guaoran.other.singleton.threadSafeTest;

import com.guaoran.other.singleton.lazy.LazyEnumSingleton;

import java.util.concurrent.CountDownLatch;

/**
 * @author : 孤傲然
 * @Description :
 *  懒汉模式，多线程安全测试
 * @date :2018/5/13 13:50
 */
public class LazyThreadTest {
    public static void main(String[] args) {
        int count = 100;
        /**
         * 使用CountDownLatch，允许一个或多个线程进行等待其他线程完成操作
         */
        CountDownLatch latch = new CountDownLatch(count);
        for (int i = 0;i<count;i++){
            new Thread(){
                @Override
                public void run() {
//                    System.out.println(System.currentTimeMillis()+"---"+LazySingleton.getInstance());
                    System.out.println(System.currentTimeMillis()+"---"+ LazyEnumSingleton.INSTANCE);

                }
            }.start();
            latch.countDown();//线程的计数器
        }
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
